<template>
  <PageWrapper v-loading="loading" loading-tip="加载中...">
    <div class="p-4 p-4-tag">
      <div class="tag">
        <BasicTable @register="registerTable">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'InfoCount' || column.key === 'ActivateCount'">
              <span class="red">
                {{ record[column.key] }}
              </span>
            </template>
          </template>
          <template #toolbar>
            <a-button type="primary" @click="onSearch"> 导出 </a-button>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageWrapper>
</template>

<script setup lang="ts">
  import { usePermission } from '/@/hooks/web/usePermission';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { BasicColumn } from '/@/components/Table/src/types/table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { getalarmrecordstatis } from '/@/api/alertRules/index';
  import { ref, h, onMounted, onBeforeUnmount } from 'vue';
  import dayjs from 'dayjs';
  let loading = ref<boolean>(false);
  const { hasPermission } = usePermission();
  let searchValue = ref('');
  let isSearch = false;
  // 计时器
  let intervalID: any = '';
  // 表单数据
  let columns: BasicColumn[] = [
    {
      title: '设备编码',
      dataIndex: 'AssetCode',
      width: 150,
    },
    {
      title: '设备名称',
      dataIndex: 'DeviceName',
      width: 150,
    },
    {
      title: '设备型号',
      dataIndex: 'ModelCode',
      width: 150,
    },
    {
      title: '设备型号名称',
      dataIndex: 'ModelName',
      width: 150,
    },
    {
      title: '设备类型',
      dataIndex: 'TypeName',
      width: 120,
    },
    {
      title: '报警总数',
      dataIndex: 'Sum',
      width: 100,
    },
    {
      title: '紧急',
      dataIndex: 'SeriousCount',
      width: 100,
    },
    {
      title: '严重',
      dataIndex: 'MajorCount',
      width: 100,
    },
    {
      title: '一般',
      dataIndex: 'GeneralCount',
      width: 100,
    },
    {
      title: '轻微',
      dataIndex: 'MinorCount',
      width: 100,
    },
    {
      title: '提示',
      dataIndex: 'InfoCount',
      width: 100,
    },
    {
      title: '开启状态',
      dataIndex: 'ActivateCount',
      width: 100,
    },
    {
      title: '关闭状态',
      dataIndex: 'CloseCount',
      width: 100,
    },
  ];
  const [registerTable, { reload, getPaginationRef, setPagination }] = useTable({
    title: '',
    api: getalarmrecordstatis,
    immediate: false,
    columns,
    canResize: false,
    showIndexColumn: true,
    striped: true,
    bordered: true,
    fetchSetting: {
      pageField: 'PageNO',
      sizeField: 'PageSize',
      totalField: 'Total',
      listField: 'Items',
    },
    beforeFetch: () => {
      return [
        {
          PageNO: getPaginationRef().current || 1,
          PageSize: getPaginationRef().pageSize || 50,
        },
      ];
    },
    afterFetch: (v) => {
      let levelObj = {
        0: '信息',
        1: '次要',
        2: '一般',
        3: '主要',
        4: '严重',
      };
      let AlarmStatusObj = {
        0: '无',
        1: '告警',
        2: '响应',
        3: '恢复',
      };
      return v.map((item) => {
        item.TriggerTime =
          item.TriggerTime && dayjs(item.TriggerTime).format('YYYY-MM-DD HH:mm:ss');
        item.RecoveryTime =
          item.RecoveryTime && dayjs(item.RecoveryTime).format('YYYY-MM-DD HH:mm:ss');
        item.ResponseTime =
          item.ResponseTime && dayjs(item.ResponseTime).format('YYYY-MM-DD HH:mm:ss');

        item.AlarmLevelCn = levelObj[item.AlarmLevel];
        item.AlarmStatusCn = AlarmStatusObj[item.AlarmStatus];
        return item;
      });
    },
  });
  const { createConfirm, createMessage } = useMessage();
  const { t } = useI18n();
  // 搜索方法
  function onSearch() {
    setPagination({
      current: 1,
    });
    reload();
  }
  // 启动轮询
  function startIntercal() {
    if (intervalID) clearInterval(intervalID);
    intervalID = setInterval(() => {
      onSearch();
    }, 15000);
  }
  onMounted(() => {
    reload();
    startIntercal();
  });
  onBeforeUnmount(() => {
    if (intervalID) clearInterval(intervalID);
  });
</script>

<style lang="less" scoped>
  .p-4-tag {
    width: 100%;
    height: 100%;
    padding: 0px !important;

    .tag {
      position: relative;
      overflow: hidden;
      height: 100%;
      background-color: #fff;
      padding-top: 20px;
    }

    .red {
      color: red;
      font-weight: 600;
    }
  }
</style>
